#include "system.h"


// This default constructor will initiate an empty system with no magnetic field.
System::System()
{
	epsilon = 0.0;		// Onsite energy [eV]
	Delta = 0.022;		// Dimer energy shift
	gamma0 = -3.16;		// Hopping energy [eV]
	gamma1 =  0.381;	// Hopping energy [eV]
	gamma3 = -0.38;		// Hopping energy [eV]
	gamma4 =  0.14;		// Hopping energy [eV]
	CurrentID = 0;
}
// This default destructor will deallocate all objects it owns.
System::~System() { eraseAllObjects(); }
	
// This holds a standard system that is always available.
System System::Std = System();
	
// This getter function will fetch the object corresponding to the ID. If the ID does not point to an object a null pointer is returned.
int System::getId(Unit *obj) const { for(auto &u: units) { if (u.second == obj) { return u.first; }} return -1; }
// This getter function will fetch the object corresponding to the ID. If the ID does not point to an object a null pointer is returned.
Unit *System::getUnit(int id) const { if(units.count(id) == 0) { return 0; } return units.at(id); }
	
// This convenience setter function will initialize all objects registered in this system.
void System::initializeAllObjects() { for(auto &u: units) { u.second->initialize(); } }

// This function will register the object and return an ID. This object is now own by the system and it will take care of deallocating.
size_t System::registerObject(Unit *unit) { units[CurrentID] = unit; CurrentID++; return CurrentID-1; }
// This function will release the object corresponding to the ID. This object is now disowned by the system and it will not deallocate it.
void System::releaseObject(int id) { if(units.count(id) > 0) { units.erase(units.find(id)); } }
// This function will release and deallocate the object corresponding to the ID.
void System::eraseObject(int id) { if(units.count(id) == 0) { return; } delete units.at(id); releaseObject(id); }
// This function will release all objects. The objects are now disowned by the system and it will not deallocate them.
void System::releaseAllObjects() { units.clear(); }
// This function will release and deallocate all objects.
void System::eraseAllObjects() { for(auto &u: units) { delete u.second; } releaseAllObjects(); }
